<script setup>
	import {
		dataSerialize,
		dataSerializeURI
	} from "@/utils/md5/index.js"
	import {
		allPages,
		needLoginPages
	} from '@/utils/router/pages'
	import {
		queryStringToObject
	} from '@/common/common'
	import {
		useUserStore
	} from '@/store/user'
	import {
		useWebviewStore
	} from '@/store/webview'
	import {
		useAppStore
	} from '@/store/app'
	// #ifdef APP
	import {
		getAppUpdate
	} from '@/utils/request/api'
	// #endif
	import {
	} from '@/utils/request/api'
	import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
	import {
		onMounted,
	} from 'vue'
	const appStore = useAppStore()
	const userStore = useUserStore()
	const webviewStore = useWebviewStore()
	onMounted(() => {
		// #ifdef APP-PLUS
			setTimeout(()=>{
				plus.navigator.closeSplashscreen()
			},1000)
		// #endif
	})
	onLaunch(async(options) => {
		// getUpdate()
		console.log(options,"App Launch");
		// #ifdef APP
		// 处理外部唤醒逻辑
		if (options.path && options.query) {
		    const path = options.path; // 获取路径，如 "pages/home/index"
		    const query = options.query; // 获取参数，如 {id: 123}
			if(path != 'pages/index/index'){
				uni.navigateTo({
				  url: `/${path}?${Object.keys(query).map(k => `${k}=${query[k]}`).join('&')}`
				});
			}
		    
		}
		// #endif
	});
	onShow((options) => {
		console.log("App Show",options);
	});
	onHide(() => {
		console.log("App Hide");
	});
	
	const getUpdate = async () => {
		
		// #ifdef MP
		const updateManager = uni.getUpdateManager();
		updateManager.onCheckForUpdate(function (res) {
		    // 请求完新版本信息的回调
		    if(res.hasUpdate){
				console.log('已获取到更新资源')
			    updateManager.onUpdateReady(function (res) {
				    uni.showModal({
				        title: '更新提示',
					    content: '新版本已经准备好，是否重启应用？',
						success(res) {
							if (res.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate();
							}
						}
					})
			    })
		    }else{
				console.log('未获取到更新资源')
			}
		})
		// #endif
		
		// #ifdef APP
		plus.runtime.getProperty(plus.runtime.appid, async function(widgetInfo) {
			
			console.log(appStore.loginSystemInfo.platform,'type传参')
			console.log(widgetInfo.name,'name传参')
			console.log(widgetInfo.version,'version传参')
			
			const res = await getAppUpdate({
				type: appStore.loginSystemInfo.platform == 'android' ? 1 : 2,
				name: widgetInfo.name,
				version: widgetInfo.version
			})
			install(res)
		})
		// #endif
	}
	const install = (resData) => {
		console.log(resData,'版本更新接口内容')
		console.log(appStore.loginSystemInfo.platform,'当前应用系统环境')
		if (resData.big_update && resData.pkgUrl) {
			//在App Store Connect中的App Store下的app信息，可找到appleId
			if (appStore.loginSystemInfo.platform == 'ios') {
				uni.showModal({
					title: "版本更新",
					content: '有新的版本发布，是否立即进行新版本下载？',
					confirmText: '立即更新',
					cancelText: '稍后进行',
					success: (res) => {
						if (res.confirm) {
							let appleId = 1663139923
							plus.runtime.launchApplication({
								action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
							}, function(e) {
								console.log(
									'Open system default browser failed: ' +
									e.message);
							});
						}
					}
				})
				return
			}
	
			if (appStore.loginSystemInfo.platform == 'android') {
				uni.showModal({
					title: '提示',
					content: '确认下载新版本?',
					success: function(res) {
						if (res.confirm) {
							if (resData.pkgUrl) {
								uni.downloadFile({
									url: resData.pkgUrl,
									success: (downloadResult) => {
										if (downloadResult.statusCode === 200) {
											plus.runtime.install(
												downloadResult.tempFilePath, {
													force: false
												},
												function() {
													console.log('install success...');
													plus.runtime.restart();
												},
												function(e) {
													console.error('install fail...');
												}
											);
										}
									}
								});
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			}
			return
		}
	
		if (resData.update && resData.wgtUrl) {
			uni.$u.toast('版本更新中...')
			uni.downloadFile({
				url: resData.wgtUrl,
				success: (downloadResult) => {
					if (downloadResult.statusCode === 200) {
						plus.runtime.install(
							downloadResult.tempFilePath, {
								force: false
							},
							function() {
								console.log('wgt install success...');
								plus.runtime.restart();
							},
							function(e) {
								console.error('wgt install fail...');
							}
						);
					}
				}
			});
			return
		}
		
	}
</script>
<style lang="scss">
	@import "@/assets/css/animate.css";
	@import "@/uni_modules/uview-plus/index.scss";
	@import '@/static/iconfont/iconfont.css';
	@import '@/static/iconfont2/iconfont.css';
	@import '@/assets/css/index.scss';
	
	.transition{
		transition: all 0.3s;
	}
	
	.public-box-1{
		background: url('/static/image/box-1.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-box-2{
		background: url('/static/image/box-2.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-box-3{
		background: url('/static/image/box-3.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-box-4{
		background: url('/static/image/box-4.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-box-5{
		background: url('/static/image/box-5.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-box-6{
		background: url('/static/image/box-6.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-box-7{
		background: url('/static/image/box-7.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-box-8{
		background: url('/static/image/box-8.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-box-9{
		background: url('/static/image/box-9.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-box-10{
		background: url('/static/image/box-10.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-box-11{
		background: url('/static/image/box-11.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-box-12{
		background: url('/static/image/box-12.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-box-13{
		background: url('/static/image/box-13.png') no-repeat;
		background-size: 100% 100%;
	}
	
	.public-input-1{
		background: url('/static/image/input-1.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-input-2{
		background: url('/static/image/input-2.png') no-repeat;
		background-size: 100% 100%;
	}
	
	
	
	.public-btn-1{
		background: url('/static/image/btn-1.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-2{
		background: url('/static/image/btn-2.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-3{
		background: url('/static/image/btn-3.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-4{
		background: url('/static/image/btn-4.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-5{
		background: url('/static/image/btn-5.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-6{
		background: url('/static/image/btn-6.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-7{
		background: url('/static/image/btn-7.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-8{
		background: url('/static/image/btn-8.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-9{
		background: url('/static/image/btn-9.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-10{
		background: url('/static/image/btn-10.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-11{
		background: url('/static/image/btn-11.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-12{
		background: url('/static/image/btn-12.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-13{
		background: url('/static/image/btn-13.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-btn-14{
		background: url('/static/image/btn-14.png') no-repeat;
		background-size: 100% 100%;
	}
	
	
	.public-img-1{
		background: url('/static/image/img-1.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-img-2{
		background: url('/static/image/img-2.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-img-3{
		background: url('/static/image/img-3.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-img-4{
		background: url('/static/image/img-4.png') no-repeat;
		background-size: 100% 100%;
	}
	
	
	
	.public-card-1{
		background: url('/static/image/card-1.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-2{
		background: url('/static/image/card-2.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-3{
		background: url('/static/image/card-3.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-4{
		background: url('/static/image/card-4.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-5{
		background: url('/static/image/card-5.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-6{
		background: url('/static/image/card-6.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-7{
		background: url('/static/image/card-7.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-8{
		background: url('/static/image/card-8.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-9{
		background: url('/static/image/card-9.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-10{
		background: url('/static/image/card-10.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-11{
		background: url('/static/image/card-11.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-12{
		background: url('/static/image/card-12.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-13{
		background: url('/static/image/card-13.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-card-14{
		background: url('/static/image/card-14.png') no-repeat;
		background-size: 100% 100%;
	}
	
	
	.public-popup-1{
		background: url('/static/image/popup-gong-gao-1.png') no-repeat;
		background-size: 100% 100%;
		min-height: 1100rpx;
	}
	.public-popup-2{
		background: url('/static/image/popup-gong-gao-2.png') no-repeat;
		background-size: 100% 100%;
		min-height: 760rpx;
	}
	.public-popup-3{
		background: url('/static/image/popup-gong-gao-3.png') no-repeat;
		background-size: 100% 100%;
		min-height: 600rpx;
	}
	.public-popup-4{
		background: url('/static/image/popup-gong-gao-4.png') no-repeat;
		background-size: 100% 100%;
		min-height: 900rpx;
	}
	.public-popup-5{
		background: url('/static/image/popup-gong-gao-5.png') no-repeat;
		background-size: 100% 100%;
		min-height: 600rpx;
	}
	
	
	
	.public-cover-1{
		background: url('/static/image/cover-1.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-cover-2{
		background: url('/static/image/cover-2.png') no-repeat;
		background-size: 100% 100%;
	}
	.public-cover-3{
		background: url('/static/image/cover-3.png') no-repeat;
		background-size: 100% 100%;
	}
	
	
	.color{
		color: var(--color);
	}
	.gray-9{
		color: var(--gray-9);
	}
</style>